<template>
	<view>
		<view class="search-box">
			<uni-search-bar cancelButton="none" :radius="100"  @input="input" ></uni-search-bar>
		</view>
		<!-- 搜索建议  -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" @click="gotoDetail(item)" v-for="(item,i) in searchResults" :key="i">
				<view class="sugg-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="17"></uni-icons>
			</view>
		</view>
		<!-- 历史记录 -->
		<view class="history-box" v-else>
			<view class="history-item">
				<!-- 标题 -->
				<view class="history-title">
					<text>搜索历史</text>
					<uni-icons type="trash" size="17" @click="clean"></uni-icons>
				</view>
				<!-- 列表区域 -->
				<view class="history-list">
					<uni-tag  @click="gotoGoodList(item)" :text="item" v-for="(item,i) in historys" :key="i"></uni-tag>
				</view>
				
			</view>
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer:'null', // 定义定时器
				kw:'' , // 关键词
			    searchResults:[]  ,// 搜索结果列表,
				historyList:['app','vue','java']
			};
		},
		methods:{
			// 输入框
			input(e){
				// 防抖功能
				clearTimeout(this.timer)
				this.timer = setTimeout(()=>{
					this.kw = e
					this.getSearchList()
				},500)
			},
		async getSearchList(){
			// 判断输入框无值的时候处理
			if(this.kw.length === 0){
				this.searchResults = []
				return
			}
			const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
			 // 状态码 ！= 200 的时候处理
			 if(res.meta.status !== 200) return uni.$showMsg()
			 // 成功的时候，赋值处理
              this.searchResults = res.message
			  this.saveSearchHistory()
			},
			// 跳转到详情页
			gotoDetail(item){
				
			var	good_id = item.goods_id
		    uni.navigateTo({
		    	url:'/subpkg/goods_detail/goods_detail?goods_id =' + good_id
		    })
			},
			saveSearchHistory(){
				var set = new Set(this.historyList)
				// 移除所有相同命名的输入值
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList = Array.from(set)
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			clean(){
				this.historyList = []
				uni.setStorageSync('kw','[]')
			},
			gotoGoodList(kw){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+kw
				})
			}
		},
		computed:{
			historys(){
				return [...this.historyList].reverse()
			}
		},
		onLoad() {
		this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}
	.sugg-list {
		padding: 0 5px;
		.sugg-item {
			display: flex;
			justify-content: space-between;
			padding: 15px 0;
			border-bottom: 1px solid #C0C0C0;
			.sugg-name {
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			
		}
		}
		
		
	}
	
	.history-box {
		padding: 0 5px;
		.history-item {
			.history-title {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 40px;
				font-size: 13px;
				border-bottom: 1px solid #efefef;
			}
			.history-list{
				display: flex;
				flex-wrap: wrap;
				.uni-tag {
					margin-top: 5px;
					margin-right: 5px;
				}
				
			}
		}
		
	}


</style>
